#region DotNetNuke License
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2006
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Reflection;

/// <summary>
/// Class for dealing with the translation of database null values.
/// </summary>
public class Null
{
    // define application encoded null values
    public static short NullShort
    {
        get
        {
            return -1;
        }
    }

    public static int NullInteger
    {
        get
        {
            return -1;
        }
    }

    public static float NullSingle
    {
        get
        {
            return float.MinValue;
        }
    }

    public static double NullDouble
    {
        get
        {
            return double.MinValue;
        }
    }

    public static decimal NullDecimal
    {
        get
        {
            return decimal.MinValue;
        }
    }
    public static decimal NullLong
    {
        get
        {
            return long.MinValue;
        }
    }

    public static DateTime NullDate
    {
        get
        {
            return DateTime.MinValue;
        }
    }

    public static string NullString
    {
        get
        {
            return "";
        }
    }

    public static bool NullBoolean
    {
        get
        {
            return false;
        }
    }

    public static Guid NullGuid
    {
        get
        {
            return Guid.Empty;
        }
    }

    // sets a field to an application encoded null value ( used in BLL layer )
    public static object SetNull(object objValue, object objField)
    {
        object returnValue;
        if (Globals.IsDBNull(objValue))
        {
            if (objField is short)
            {
                returnValue = NullShort;
            }
            else if (objField is int)
            {
                returnValue = NullInteger;
            }
            else if (objField is Single)
            {
                returnValue = NullSingle;
            }
            else if (objField is double)
            {
                returnValue = NullDouble;
            }
            else if (objField is decimal)
            {
                returnValue = NullDecimal;
            }
            else if (objField is DateTime)
            {
                returnValue = NullDate;
            }
            else if (objField is string)
            {
                returnValue = NullString;
            }
            else if (objField is bool)
            {
                returnValue = NullBoolean;
            }
            else if (objField is Guid)
            {
                returnValue = NullGuid;
            }
            else // complex object
            {
                returnValue = null;
            }
        }
        else // return value
        {
            returnValue = objValue;
        }
        return returnValue;
    }

    // sets a field to an application encoded null value ( used in BLL layer )
    public static object SetNull(PropertyInfo objPropertyInfo)
    {
        object returnValue;
        switch (objPropertyInfo.PropertyType.ToString())
        {
            case "System.Int16":

                returnValue = NullShort;
                break;
            case "System.Int32":
                returnValue = NullInteger;
                break;

            case "System.Int64":

                returnValue = NullInteger;
                break;
            case "System.Single":

                returnValue = NullSingle;
                break;
            case "System.Double":

                returnValue = NullDouble;
                break;
            case "System.Decimal":

                returnValue = NullDecimal;
                break;
            case "System.DateTime":

                returnValue = NullDate;
                break;
            case "System.String":
                returnValue = NullString;
                break;

            case "System.Char":

                returnValue = NullString;
                break;
            case "System.Boolean":

                returnValue = NullBoolean;
                break;
            case "System.Guid":

                returnValue = NullGuid;
                break;
            default:

                // Enumerations default to the first entry
                Type pType = objPropertyInfo.PropertyType;
                if (pType.BaseType.Equals(typeof(Enum)))
                {
                    Array objEnumValues = Enum.GetValues(pType);
                    Array.Sort(objEnumValues);
                    returnValue = Enum.ToObject(pType, objEnumValues.GetValue(0));
                }
                else // complex object
                {
                    returnValue = null;
                }
                break;
        }
        return returnValue;
    }

    // convert an application encoded null value to a database null value ( used in DAL )
    public static object GetNull(object objField, object objDBNull)
    {
        object returnValue;
        returnValue = objField;
        if (objField == null)
        {
            returnValue = objDBNull;
        }
        else if (objField is short)
        {
            if (Convert.ToInt16(objField) == NullShort)
            {
                returnValue = objDBNull;
            }
        }
        else if (objField is int)
        {
            if (Convert.ToInt32(objField) == NullInteger)
            {
                returnValue = objDBNull;
            }
        }
        else if (objField is Single)
        {
            if (Convert.ToSingle(objField) == NullSingle)
            {
                returnValue = objDBNull;
            }
        }
        else if (objField is double)
        {
            if (Convert.ToDouble(objField) == NullDouble)
            {
                returnValue = objDBNull;
            }
        }
        else if (objField is decimal)
        {
            if (Convert.ToDecimal(objField) == NullDecimal)
            {
                returnValue = objDBNull;
            }
        }
        else if (objField is DateTime)
        {
            // compare the Date part of the DateTime with the DatePart of the NullDate ( this avoids subtle time differences )
            if (Convert.ToDateTime(objField).Date == NullDate.Date)
            {
                returnValue = objDBNull;
            }
        }
        else if (objField is string)
        {
            if (objField == null)
            {
                returnValue = objDBNull;
            }
            else
            {
                if (objField.ToString() == NullString)
                {
                    returnValue = objDBNull;
                }
            }
        }
        else if (objField is bool)
        {
            if (Convert.ToBoolean(objField) == NullBoolean)
            {
                returnValue = objDBNull;
            }
        }
        else if (objField is Guid)
        {
            if (((Guid)objField).Equals(NullGuid))
            {
                returnValue = objDBNull;
            }
        }
        return returnValue;
    }

    // checks if a field contains an application encoded null value
    public static bool IsNull(object objField)
    {
        bool returnValue;
        if (objField != null)
        {
            if (objField is int)
            {
                returnValue = objField.Equals(NullInteger);
            }
            else if (objField is Single)
            {
                returnValue = objField.Equals(NullSingle);
            }
            else if (objField is double)
            {
                returnValue = objField.Equals(NullDouble);
            }
            else if (objField is decimal)
            {
                returnValue = objField.Equals(NullDecimal);
            }
            else if (objField is DateTime)
            {
                DateTime objDate = Convert.ToDateTime(objField);
                returnValue = objDate.Date.Equals(NullDate.Date);
            }
            else if (objField is string)
            {
                returnValue = objField.Equals(NullString);
            }
            else if (objField is bool)
            {
                returnValue = objField.Equals(NullBoolean);
            }
            else if (objField is Guid)
            {
                returnValue = objField.Equals(NullGuid);
            }
            else // complex object
            {
                returnValue = false;
            }
        }
        else
        {
            returnValue = true;
        }
        return returnValue;
    }

    private static T DirectCast<T>(object o)
    {
        return (T)o;
    }

    public static T GetNullOfType<T>()
    {
        Type type = typeof(T);
        if (type == typeof(short))
        {
            return DirectCast<T>(Null.NullShort);
        }
        else if (type == typeof(int))
        {
            return DirectCast<T>(Null.NullInteger);
        }
        else if (type == typeof(Single))
        {
            return DirectCast<T>(Null.NullSingle);
        }
        else if (type == typeof(Double))
        {
            return DirectCast<T>(Null.NullDouble);
        }
        else if (type == typeof(Decimal))
        {
            return DirectCast<T>(Null.NullDecimal);
        }
        else if (type == typeof(DateTime))
        {
            return DirectCast<T>(Null.NullDate);
        }
        else if (type == typeof(string))
        {
            return DirectCast<T>(Null.NullString);
        }
        else if (type == typeof(bool))
        {
            return DirectCast<T>(Null.NullBoolean);
        }
        else if (type == typeof(Guid))
        {
            return DirectCast<T>(Null.NullGuid);
        }
        else if (type == typeof(object))
        {
            return DirectCast<T>(null);
        }
        return default(T);
    }
}
